package com.liuzhilong.infrastructure.framework.algorithms.algorithms.sort.base;

import static com.liuzhilong.infrastructure.framework.algorithms.algorithms.sort.SortCommon.*;

/**
 * <h1>选择排序</h1>
 * <p>
 * 选择排序的逻辑是循环遍历数组，找到最小的数放在前面.
 * </p>
 */
public class SelectionSort {


    public static final void sort(Comparable[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            int base = i;
            //找到最小的数据
            for (int j = i+1; j <arrays.length; j++) {
                if(less(arrays[j],arrays[base])){
                    base=j;
                }
            }
            //当前的游标与最小的交换
            exch(arrays,i,base);
        }
    }


}
